﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Exceptions;
using LibNiconico.NiconicoApi.Videos;
using LibNiconico.NiconicoApi.Videos.Constant;


namespace LibNiconico.NiconicoApi.Utils
{
    /// <summary>
    /// ニコニコAPI認証情報
    /// </summary>
    internal sealed class NiconicoAuth
    {
        internal Cookie UserSession
        {
            get;
            private set;
        }

        private const string userSessionName = "user_session";

        /// <summary>
        /// コンストラクタ
        /// </summary>
        internal NiconicoAuth()
        {
            UserSession = new Cookie();
        }

        /// <summary>
        /// 認証情報が設定されているか判定
        /// </summary>
        /// <returns></returns>
        internal bool IsSetAuth
        {
            get
            {
                return IsEnableUserSession(UserSession);
            }
            
        }

        /// <summary>
        /// ユーザセッションを取得
        /// </summary>
        /// <returns></returns>
        internal Cookie GetUserSession()
        {
            return GetUserSession(this.UserSession);
        }



        /// <summary>
        /// ユーザセッションを設定
        /// </summary>
        /// <param name="userSession"></param>
        internal void SetUserSession(CookieContainer userSession)
        {
            SetCookie(userSession);
        }

        /// <summary>
        /// ユーザセッションを設定
        /// </summary>
        /// <param name="userSession"></param>
        internal void SetUserSession(Cookie userSession)
        {
            SetCookie(userSession);
        }

        private static Cookie GetUserSession(Cookie cookie)
        {
            return cookie;
        }

        /// <summary>
        /// クッキーがニコニコ認証情報を含んでいるか判定
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        internal static bool IsEnableUserSession(Cookie value)
        {
            if (GetUserSession(value) != null)
            {
                return value.Name == userSessionName;
            }
            else
            {
                return false;
            }
            
        }

        /// <summary>
        /// クッキーがニコニコ認証情報を含んでいるか判定
        /// </summary>
        /// <param name="cookieContainer"></param>
        /// <returns></returns>
        internal static bool IsLoggedIn(CookieContainer cookieContainer)
        {
            var cookies = cookieContainer.GetCookies(NicoVideoConstant.NICOVIDEO_URL);
            return cookies[userSessionName] != null;
        }

        /// <summary>
        /// 認証情報をクリア
        /// </summary>
        internal void Clear()
        {
            this.UserSession = new Cookie();
        }

        /// <summary>
        /// クッキーを追加
        /// </summary>
        /// <param name="cookieContainer"></param>
        internal void SetCookie(CookieContainer cookieContainer)
        {
            SetCookie(cookieContainer.GetCookies(NicoVideoConstant.NICOVIDEO_URL));
        }

        /// <summary>
        /// クッキーを追加
        /// </summary>
        /// <param name="cookieCollection"></param>
        internal void SetCookie(CookieCollection cookieCollection)
        {
            foreach (Cookie cookie in cookieCollection)
            {
                SetCookie(cookie);

            }
            
        }

        /// <summary>
        /// クッキーを追加
        /// </summary>
        /// <param name="cookie"></param>
        internal void SetCookie(Cookie cookie)
        {
            UserSession = cookie;

        }




        
    }
}
